package com.mimc.controller.api;

import com.jfinal.aop.Inject;
import com.jfinal.upload.UploadFile;
import com.mimc.core.model.ImUser;
import com.mimc.core.service.ImUserService;
import com.mimc.framework.domain.AjaxResult;
import com.mimc.framework.jwt.ApiTokenUtils;
import com.mimc.framework.utils.TranslateUtil;
import com.mimc.service.ShMimcService;
import com.mimc.service.ShUserService;
import io.jboot.utils.StrUtil;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import java.util.Date;

@RequestMapping("/api/user")
public class UserController extends JbootController {
    @Inject
    private ImUserService userService;
    @Inject
    private ShUserService shUserService;
    @Inject
    private ShMimcService shMimcService;
    public AjaxResult getUserInfo(){
        //更新语言
        ImUser user=ApiTokenUtils.getCurrentUser();
        if(ApiTokenUtils.getLang()!=null){
            user.setLang(ApiTokenUtils.getLang());
            userService.update(user);
        }
        return AjaxResult.success("",user);
    }
    //切换语言
    public AjaxResult changeUserLang(){
        if(ApiTokenUtils.getLang()!=null){
            ImUser user=ApiTokenUtils.getCurrentUser();
            user.setLang(ApiTokenUtils.getLang());
            userService.update(user);
        }
        return AjaxResult.success("");
    }
    //更新头像
    public AjaxResult uploadAvatar(){
        UploadFile uploadFile=getFile();
        boolean flag=shUserService.uploadAvatar(uploadFile);
        if(flag){
            return AjaxResult.success(TranslateUtil.translate("上传成功"));
        }else{
            return AjaxResult.success(TranslateUtil.translate("上传失败"));
        }
    }
    public AjaxResult updateUser(){
        ImUser user=ApiTokenUtils.getCurrentUser();
        String nickname=getPara("nickname");
        String gender=getPara("gender");
        String intro=getPara("intro");
        String sign=getPara("sign");
        Date birthday=getDate("birthday");
        if(StrUtil.isNotEmpty(nickname)){
            user.setNickname(nickname);
        }
        if(StrUtil.isNotEmpty(gender)){
            user.setGender(gender);
        }
        if(StrUtil.isNotEmpty(intro)){
            user.setIntro(intro);
        }
        if(StrUtil.isNotEmpty(intro)){
            user.setIntro(intro);
        }
        if(StrUtil.isNotEmpty(sign)){
            user.setSign(sign);
        }
        if(StrUtil.notNull(birthday)){
            user.setBirthday(birthday);
        }
        userService.update(user);
        return AjaxResult.success();
    }
    //获取IM登录token
    public AjaxResult getImToken(){
        Long userId=ApiTokenUtils.getCurrentUserId();
        Boolean cache=getBoolean("cache",true);
        String token=shMimcService.getToken(userId,cache);
        return AjaxResult.success("",token);
    }

    public AjaxResult detail(Long userId){
        ImUser user= userService.findById(userId);
        user.setPassword("");
        return AjaxResult.success("",user);
    }
    //获取提醒
    public AjaxResult getReminds(){
        return AjaxResult.error("");
    }
}
